Python Testing with pytest by Brian Okken

Python Testing with pytest by Brian Okken

Author:Brian Okken
Language: eng
Format: epub, pdf
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf


Using capsys

The capsys builtin fixture provides two bits of functionality: it allows you to retrieve stdout and stderr from some code, and it disables output capture temporarily. Let’s take a look at retrieving stdout and stderr.

Suppose you have a function to print a greeting to stdout:

ch4/cap/test_capsys.py

​ ​def​ greeting(name):

​ ​print​(​'Hi, {}'​.format(name))

You can’t test it by checking the return value. You have to test stdout somehow. You can test the output by using capsys:

ch4/cap/test_capsys.py

​ ​def​ test_greeting(capsys):

​ greeting(​'Earthling'​)

​ out, err = capsys.readouterr()

​ ​assert​ out == ​'Hi, Earthling​​\n​​'​

​ ​assert​ err == ​''​

​ greeting(​'Brian'​)

​ greeting(​'Nerd'​)

​ out, err = capsys.readouterr()

​ ​assert​ out == ​'Hi, Brian​​\n​​Hi, Nerd​​\n​​'​

​ ​assert​ err == ​''​

The captured stdout and stderr are retrieved from capsys.redouterr(). The return value is whatever has been captured since the beginning of the function, or from the last time it was called.

The previous example only used stdout. Let’s look at an example using stderr:

ch4/cap/test_capsys.py

​ ​def​ yikes(problem):

​ ​print​(​'YIKES! {}'​.format(problem), file=sys.stderr)

​ ​def​ test_yikes(capsys):

​ yikes(​'Out of coffee!'​)

​ out, err = capsys.readouterr()

​ ​assert​ out == ​''​

​ ​assert​ ​'Out of coffee!'​ ​in​ err

pytest usually captures the output from your tests and the code under test. This includes print statements. The captured output is displayed for failing tests only after the full test session is complete. The -s option turns off this feature, and output is sent to stdout while the tests are running. Usually this works great, as it’s the output from the failed tests you need to see in order to debug the failures. However, you may want to allow some output to make it through the default pytest output capture, to print some things without printing everything. You can do this with capsys. You can use capsys.disabled() to temporarily let output get past the capture mechanism.

Here’s an example:

ch4/cap/test_capsys.py

​ ​def​ test_capsys_disabled(capsys):

​ ​with​ capsys.disabled():

​ ​print​(​'​​\n​​always print this'​)

​ ​print​(​'normal print, usually captured'​)

Now, ’always print this’ will always be output:

​ ​$ ​​cd​​ ​​/path/to/code/ch4/cap​

​ ​$ ​​pytest​​ ​​-q​​ ​​test_capsys.py::test_capsys_disabled​

​ always print this

​ .

​ 1 passed in 0.01 seconds

​ ​$ ​​pytest​​ ​​-q​​ ​​-s​​ ​​test_capsys.py::test_capsys_disabled​

​ always print this

​ normal print, usually captured

​ .

​ 1 passed in 0.00 seconds

As you can see, always print this shows up with or without output capturing, since it’s being printed from within a with capsys.disabled() block. The other print statement is just a normal print statement, so normal print, usually captured is only seen in the output when we pass in the -s flag, which is a shortcut for --capture=no, turning off output capture.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.